package com.zrrd.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.common.result.Result;
import com.zrrd.model.product.SkuInfo;
import com.zrrd.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    //分页查询sku列表
    @GetMapping("/list/{page}/{limit}")
    public Result<Page> list(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit){
        //创建分页对象
        Page pageInfo = new Page(page,limit);
        //执行分页
        skuInfoService.page(pageInfo,null);
        return Result.ok(pageInfo);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result<String> onSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo  = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
        return Result.ok("上架成功");
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result<String> cancelSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo  = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
        return Result.ok("下架成功");
    }

    //sku保存
    @PostMapping("/saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok("添加成功");
    }

    //
}
